package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.SearchMV;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 李海龙 on 15/10/27.
 */
public class SearchMVListViewAdapter extends BaseAdapter {

    private Context context;
    private List<SearchMV.DataEntity> dataEntities;

    public SearchMVListViewAdapter(List<SearchMV.DataEntity> dataEntities, Context context) {
        this.dataEntities = dataEntities;
        this.context = context;
    }

    public void loadData(List<SearchMV.DataEntity> dataEntities){
        this.dataEntities.addAll(dataEntities);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return dataEntities != null && dataEntities.size() > 0 ? dataEntities.size() : 0;
    }

    @Override
    public SearchMV.DataEntity getItem(int position) {
        return dataEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.search_mv_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SearchMV.DataEntity dataEntity = getItem(position);
        holder.mvTitle.setText(dataEntity.getVideoName());
        holder.mvCommentCountTv.setText(dataEntity.getBulletCount() + "");
        holder.mvPlayCountTv.setText(dataEntity.getPickCount() + "");
        holder.mvSingerName.setText(dataEntity.getSingerName());


//        Picasso.with(context).load(data.getPic_url()).placeholder(R.mipmap.placeholder_disk_300)
//                .error(R.mipmap.placeholder_disk_300).into(viewHolder.songSheetItemImg);

        Picasso.with(context).load(dataEntity.getPicUrl()).placeholder(R.mipmap.placeholder_detail_radio_ban)
                .error(R.mipmap.placeholder_detail_radio_ban).into(holder.mvPicture);
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'search_mv_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.mv_picture)
        ImageView mvPicture;
        @Bind(R.id.mv_title)
        TextView mvTitle;
        @Bind(R.id.mv_singerName)
        TextView mvSingerName;
        @Bind(R.id.mv_playCountIv)
        ImageView mvPlayCountIv;
        @Bind(R.id.mv_playCountTv)
        TextView mvPlayCountTv;
        @Bind(R.id.mv_commentCountTv)
        TextView mvCommentCountTv;
        @Bind(R.id.mv_commentCountIv)
        ImageView mvCommentCountIv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
